//
//  SYVistorView.swift
//  YANAWeibo
//
//  Created by mac on 16/5/12.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

class SYVistorView: UIView {
    
    var callBackClosure: (()->())?
    
    //懒加载控件
    //旋转图片
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //创建遮罩
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //icon
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //设置文本信息
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人,回这里看看哟什么惊喜关注一些人,回这里看看哟什么惊喜"
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        return label
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "registerAction", forControlEvents: .TouchUpInside)
        button.setTitle("注册", forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        return button
        
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "loginAction", forControlEvents: .TouchUpInside)
        button.setTitle("登录", forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        return button
        
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1.0)
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //设置约束
        cycleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cycleImageView)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(cycleImageView)
            make.top.equalTo(cycleImageView.snp_bottom)
            make.size.equalTo(CGSize(width: 224, height: 40))
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 35))
            
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
            make.right.equalTo(messageLabel)
        }
    }
    
    @objc private func registerAction() {
        callBackClosure?()
    }
    
    @objc private func loginAction() {
        callBackClosure?()
    }
    
    //开始动画
    private func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //目的地
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        //防止动画释放掉
        animation.removedOnCompletion = false
        cycleImageView.layer.addAnimation(animation, forKey: nil)
    }
    
    //修改访客视图信息
    func setVistorInfo(title: String?, imageName: String?) {
        if let t = title, img = imageName {
            //进入的不是home
            messageLabel.text = t
            iconImageView.image = UIImage(named: img)
            //隐藏动画
            cycleImageView.hidden = true
        }else{
        
            cycleImageView.hidden = false
            startAnimation()
        }
    }
    
    
    
    
    
    
    
    
    
}
